package sessionBeanTest;


import static org.junit.Assert.*;

import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.persistence.Query;

import interfaceBeanRemote.sessionParticipationRemote;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import entityBean.Club;
import entityBean.Participation;

public class ParticipationTest {
    Context ctx2;
    sessionParticipationRemote  participationbean;
	@Before
	public void setUp() throws Exception {
		ctx2= new InitialContext();
		
		ctx2.addToEnvironment(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
        ctx2.addToEnvironment(InitialContext.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
        ctx2.addToEnvironment(InitialContext.PROVIDER_URL,"jnp://localhost:1099");
        
		participationbean=(sessionParticipationRemote) ctx2.lookup("sessionParticipation/remote");
	}

 
	
	
	@After
	public void tearDown() throws Exception {
		
	}
	
	@Test
	public void testFindParticipation(){
		Participation participation=new Participation();
		participation.setMontantParticipation(1000);
		participationbean.addParticipation(participation);
		
		Participation participation2=participationbean.findParticipation(1);
		assertTrue(participation2.getMontantParticipation()==1000);
		
	}
	@Test 
	 public void testAddParticipation(){
		
		Participation participation=new Participation();
		participationbean.addParticipation(participation);
		participation.setMontantParticipation(2000);
		Participation participation3=participationbean.findParticipation(2);

		assertTrue(participation3.getIdParticipation()==2);
		
		
	}
	/*
	@Test
	public void testRemoveParticipationById(){
		
		Participation participation=new Participation();
		participationbean.addParticipation(participation);
		participationbean.removeParticipationById(3);
		assertTrue(participationbean.findParticipation(3)==null);
	}
	*/
	
	@Test
	public void testRemoveParticipation(){
		Participation participation=new Participation();
		participationbean.addParticipation(participation);
		participationbean.removeParticipation(participation);
		assertTrue(participationbean.findParticipation(4)==null);
		
		
	}
	@Test
	public void testUpdateParticipation(){
		Participation participation1=new Participation();
		participationbean.addParticipation(participation1);
		
		Participation participation2=new Participation();
		participationbean.addParticipation(participation2);
	}
	
	@Test
	public void testGetParticipationListWithInvestisseur(){
		//A FINIR
	}
	
	@Test
	public void testGetParticipationListWithInvestisseur2(){
		//A FINIR
	}
	
}

/*
 * else if (linkedAngel.equals("engagement_participation")) {

   String idStartUp = request.getParameter("str_id");
   String montantParticipation = request.getParameter("montant");

   HttpSession s = request.getSession();
   int id_inv = (Integer) s.getAttribute("id_Inv");

   Startup startUp = findStartUp(Integer.parseInt(idStartUp));

   Investisseur inves = findInvestisseur(id_inv);
   int capitalrestant = inves.getCapital()
     - Integer.parseInt(montantParticipation);
   inves.setCapital(capitalrestant);
   updateInvestisseur(inves);
   Participation p = new Participation();

   p.setMontantParticipation(Integer.parseInt(montantParticipation));

   p.setInvestisseur(inves);
   p.setStartup(startUp);

   
   System.out.println("montant : " + capitalrestant);
   // ////////////////////////////////////////////////

   participations.add(p);
   addParticipation(p);
   
   
   @SuppressWarnings("unchecked")
	@Override
	public List<Participation> getParticipationListWithInvestisseur(int idStartUp) {
		// TODO Auto-generated method stub
		Query query = em.createQuery("SELECT p FROM Participation AS p inner join fetch p.investisseur"
				+ " WHERE p.startup = " + idStartUp + " ORDER BY p.idParticipation ASC");
		return query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Participation> getParticipationListWithInvestisseur2(int idInv) {
		// TODO Auto-generated method stub
		Query query = em.createQuery("SELECT p FROM Participation AS p inner join fetch p.startup"
				+ " WHERE p.investisseur.idInvestisseur = " + idInv + " ORDER BY p.idParticipation ASC");
		return query.getResultList();
	}
*/
